//
//  ViewController.swift
//  Swift_AnimationDemo
//
//  Created by lantaiyuan on 2017/8/28.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    
    var postion = true
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "animation"
    }

    @IBAction func animationAction(_ sender: UIButton) {
        
        /* Example 01
         
         let animation:CABasicAnimation = CABasicAnimation(keyPath:"position")
         animation.fromValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:imageView.frame.midY))
         animation.toValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:view.frame.height-80))
         animation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
         animation.duration = 1.0
         imageView.layer.add(animation, forKey: "position")
         
         */
        /* example 02
         
         if postion {
         let animation:CABasicAnimation! = CABasicAnimation(keyPath:"position")
         animation.toValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:imageView.frame.midY + 40))
         
         animation.fillMode = kCAFillModeForwards
         animation.isRemovedOnCompletion = false
         
         
         let resizeAnimation:CABasicAnimation = CABasicAnimation(keyPath:"bounds.size")
         resizeAnimation.toValue = NSValue(cgSize:CGSize(width:120,height:40))
         resizeAnimation.fillMode = kCAFillModeForwards
         resizeAnimation.isRemovedOnCompletion = false
         imageView.layer.add(animation, forKey: "position")
         imageView.layer.add(resizeAnimation, forKey: "bounds.size")
         
         postion = false
         }
         else {
         
         let animation:CABasicAnimation! = CABasicAnimation(keyPath:"position")
         animation.fromValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:imageView.frame.midY))
         
         //EXAMPLE2
         animation.fillMode = kCAFillModeForwards
         animation.isRemovedOnCompletion = false
         let resizeAnimation:CABasicAnimation = CABasicAnimation(keyPath:"bounds.size")
         resizeAnimation.fromValue = NSValue(cgSize:CGSize(width: 120, height: 40))
         
         //EXAMPLE2
         resizeAnimation.fillMode = kCAFillModeForwards
         resizeAnimation.isRemovedOnCompletion = false
         imageView.layer.add(animation, forKey: "position")
         imageView.layer.add(resizeAnimation, forKey: "bounds.size")
         
         postion = true
         
         }
        */
        
        /* Example 03
         
         if postion {
         let animation:CABasicAnimation! = CABasicAnimation(keyPath:"position")
         animation.toValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:imageView.frame.midY + 40))
         
         animation.fillMode = kCAFillModeForwards
         animation.isRemovedOnCompletion = false
         
         
         let resizeAnimation:CABasicAnimation = CABasicAnimation(keyPath:"bounds.size")
         resizeAnimation.toValue = NSValue(cgSize:CGSize(width:120,height:40))
         resizeAnimation.fillMode = kCAFillModeForwards
         resizeAnimation.isRemovedOnCompletion = false
         weak var weakSelf = self
         UIView.animate(withDuration: 5.0, animations: {
         self.imageView.alpha = 0
         }, completion: { (value) in
         weakSelf!.imageView.alpha = 1.0
         weakSelf!.imageView.layer.add(animation, forKey: "position")
         weakSelf!.imageView.layer.add(resizeAnimation, forKey: "bounds.size")
         })
         
         postion = false
         }
         else {
         
         let animation:CABasicAnimation! = CABasicAnimation(keyPath:"position")
         animation.fromValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:imageView.frame.midY))
         
         //EXAMPLE2
         animation.fillMode = kCAFillModeForwards
         animation.isRemovedOnCompletion = false
         let resizeAnimation:CABasicAnimation = CABasicAnimation(keyPath:"bounds.size")
         resizeAnimation.fromValue = NSValue(cgSize:CGSize(width: 120, height: 40))
         
         //EXAMPLE2
         resizeAnimation.fillMode = kCAFillModeForwards
         resizeAnimation.isRemovedOnCompletion = false
         imageView.layer.add(animation, forKey: "position")
         imageView.layer.add(resizeAnimation, forKey: "bounds.size")
         
         postion = true
         }
         */
        
        if postion {
            let subLayer : CALayer = self.imageView.layer
            let thePath : CGMutablePath = CGMutablePath()
            
            thePath.move(to: CGPoint(x:imageView.frame.midX,y:imageView.frame.midY))
            thePath.addCurve(to: CGPoint(x:imageView.frame.midX,y:imageView.frame.midY), control1: CGPoint(x:imageView.frame.midX,y:imageView.frame.midY + 40), control2: CGPoint(x:imageView.frame.midX,y:imageView.frame.midY + 40), transform: CGAffineTransform.init())
            let theAnimation: CAKeyframeAnimation = CAKeyframeAnimation(keyPath:"position")
            theAnimation.path = thePath
            theAnimation.duration = 5.0
            theAnimation.fillMode = kCAFillModeForwards
            theAnimation.isRemovedOnCompletion = false
            
            let resizeAnimation:CABasicAnimation = CABasicAnimation(keyPath:"bounds.size")
            resizeAnimation.toValue = NSValue(cgSize:CGSize(width: 120, height: 60))
            
            //SAMPLE 2
            resizeAnimation.duration = 5.0
            resizeAnimation.fillMode = kCAFillModeForwards
            resizeAnimation.isRemovedOnCompletion = false
            subLayer.add(theAnimation, forKey: "position")
            imageView.layer.add(resizeAnimation, forKey: "bounds.size")
            
            postion = false
        } else {
            let animation:CABasicAnimation! = CABasicAnimation(keyPath:"position")
            animation.fromValue = NSValue(cgPoint:CGPoint(x:imageView.frame.midX,y:imageView.frame.midY))
            
            //SAMPLE 2
            animation.fillMode = kCAFillModeForwards
            animation.isRemovedOnCompletion = false
            let resizeAnimation:CABasicAnimation = CABasicAnimation(keyPath:"bounds.size")
            resizeAnimation.fromValue = NSValue(cgSize:CGSize(width: 120, height: 60))
            
            //SAMPLE 2
            resizeAnimation.fillMode = kCAFillModeForwards
            resizeAnimation.isRemovedOnCompletion = false
            imageView.layer.add(animation, forKey: "position")
            imageView.layer.add(resizeAnimation, forKey: "bounds.size")
            
            postion = true
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

